#include "game_window.h"

#include <QJsonArray>
#include <QMessageBox>
#include "table_item.h"
#include "util.h"
using namespace dymy;
void GameWindow::set_message_to_widget(const TabaleData &data)
{
    TabaleData *table_data_ptr = new TabaleData(data);

    TabaleData &table_data = *table_data_ptr;

    cards3_widget->set_cards(table_data.cards3);

    if(prev_player_widget)
    {
        int prev_index = my_prev_index();
        if(prev_index<table_data.users.size())
        {
            const TableUser *pu = &(table_data.users[prev_index]);
            prev_player_widget->setUserInfo(pu,table_data.prev_play_max_card_index==prev_index?table_data.current_play_max_cards:QVector<CARD_t>());
        }
    }
    {
        int index = my_index();
        if(index<table_data.users.size())
        {
            const TableUser *pu = &(table_data.users[index]);
            current_player_widget->setUserInfo(pu,table_data.prev_play_max_card_index==index?table_data.current_play_max_cards:QVector<CARD_t>());
        }
    }
    if(next_player_widget)
    {
        int next_index = my_next_index();
        if(next_index<table_data.users.size())
        {
            const TableUser *pu = &(table_data.users[next_index]);
            next_player_widget->setUserInfo(pu,table_data.prev_play_max_card_index==next_index?table_data.current_play_max_cards:QVector<CARD_t>());
        }
    }

    m_table_data.reset(table_data_ptr);
}

GameWindow::GameWindow(QWidget *parent, QWebSocket *socket, int id,int index):QDialog(parent),SocketHelper(socket),GameHelper(socket,id,index),m_table_data()
{
    setAttribute(Qt::WA_DeleteOnClose);

    resize(800,500);

    setWindowTitle(QString("牌桌%1").arg(table_id));

    container = new QHBoxLayout(this);

    container_game = new FrameLayout();

    cards3_widget = new CardPlayWidget(this);
    cards3_widget->setEnabled(false);
    container_game->addWidget(cards3_widget,Qt::AlignTop|Qt::AlignHCenter);

    prev_player_widget = new TableOtherUserWidget(this);
    prev_player_widget->setEnabled(false);
    prev_player_widget->setLayoutDirection(Qt::LayoutDirection::LeftToRight);
    container_game->addWidget(prev_player_widget,Qt::AlignVCenter|Qt::AlignLeft);

    current_player_widget = new TableSelfWidget(this);
    container_game->addWidget(current_player_widget,Qt::AlignBottom|Qt::AlignHCenter);

    connect(current_player_widget,&TableSelfWidget::prepare_clicked,this,&GameWindow::on_prepare_clicked);
    connect(current_player_widget,&TableSelfWidget::rob_clicked,this,&GameWindow::on_rob_clicked);
    connect(current_player_widget,&TableSelfWidget::cards_clicked,this,&GameWindow::on_cards_clicked);
    connect(current_player_widget,&TableSelfWidget::no_cards_clicked,this,&GameWindow::on_no_cards_clicked);

    next_player_widget = new TableOtherUserWidget(this);
    next_player_widget->setEnabled(false);
    next_player_widget->setLayoutDirection(Qt::LayoutDirection::RightToLeft);
    container_game->addWidget(next_player_widget,Qt::AlignVCenter|Qt::AlignRight);

    container->addLayout(container_game,1);

    container_chat = new QVBoxLayout();

    label_chat = new QTextEdit(this);
    label_chat->setReadOnly(true);
    label_chat->setFixedWidth(200);

    container_chat->addWidget(label_chat);

    QHBoxLayout *container_send = new QHBoxLayout();
    edit_input = new QLineEdit(this);
    connect(edit_input,&QLineEdit::returnPressed,this,std::bind(&GameWindow::on_send_clicked,this,false));

    button_chat = new QPushButton(this);
    button_chat->setText(tr("发送"));
    button_chat->setFixedWidth(50);
    connect(button_chat,&QPushButton::clicked,this,&GameWindow::on_send_clicked);

    container_send->addWidget(edit_input);
    container_send->addWidget(button_chat);

    container_chat->addLayout(container_send);

    container->addLayout(container_chat);

    connect(websocket,&QWebSocket::connected,this,&GameWindow::on_connected);
    connect(websocket,&QWebSocket::disconnected,this,&GameWindow::on_disconnect);

    connect(websocket,&QWebSocket::textMessageReceived,this,static_cast<void (GameWindow::*)(const QString &)>(&GameWindow::on_message));
    _send_join_table_message(id,index,1);
}

void GameWindow::on_message(const QString &message){
    GameHelper::on_message(message);
}

void GameWindow::on_message(const QByteArray &message){
    GameHelper::on_message(message);
}

void GameWindow::on_disconnect(){
    GameHelper::on_disconnect();
}

void GameWindow::on_connected(){
    GameHelper::on_connected();
}

void GameWindow::on_prepare_clicked(bool prepare)
{
    _send_prepare_table_message(table_id,table_index,prepare);
}

void GameWindow::on_rob_clicked(bool rob)
{
    _send_rob_table_message(table_id,table_index,rob);
}

void GameWindow::on_cards_clicked(const QVector<CARD_t> &cards)
{
    _send_cards_table_message(table_id,table_index,cards);
}
void GameWindow::on_no_cards_clicked()
{
    _send_cards_table_message(table_id,table_index,QVector<CARD_t>());
}

GameWindow::~GameWindow()=default;

void GameWindow::on_send_clicked(bool)
{
    QString text = edit_input->text().trimmed();
    if(text.isEmpty())
    {
        return;
    }
    _send_chat_table_message(text);
    edit_input->clear();
}

void GameWindow::on_game_response_error(const int &code, const int &rep_code, const QString &rep_message, const QString &tag)
{
    QMessageBox::warning(this,"提示",rep_message);
}

void GameWindow::on_add_table_message(const TabaleData &data)
{
    set_message_to_widget(data);
}

void GameWindow::on_prepare_table_message(const TabaleData &data)
{
    set_message_to_widget(data);
}

void GameWindow::on_rob_table_message(const TabaleData &data)
{
    set_message_to_widget(data);
}

void GameWindow::on_cards_table_message(const TabaleData &data)
{
    set_message_to_widget(data);
}

void GameWindow::on_chat_table_message(const ChatTableData &cdata)
{
    if(m_chat_content.length()>0)
    {
        m_chat_content.append('\n');
    }

    m_chat_content.append(format_unix_time(cdata.content.time));
    m_chat_content.append(cdata.name).append("说").append(':').append(cdata.content.message);

    label_chat->setText(m_chat_content);
    label_chat->moveCursor(QTextCursor::End);
}



void GameWindow::closeEvent(QCloseEvent *e)
{
    _send_join_table_message(table_id,table_index,0);
    QDialog::closeEvent(e);
}

